// 面试题 02.07. 链表相交 https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci/
func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
    if headA == nil || headB == nil { return nil }
    var p = headA, q = headB
    while p !== q {
        p = p == nil ? headB : p?.next
        q = q == nil ? headA : q?.next // 不能.next是防止一直循环下去 nil === nil
    }
    return p
}

var head = ListNode(1)
head.next = ListNode(2)
head.next?.next = ListNode(3)
var head1 = ListNode(7)
head1.next = ListNode(8)
head1.next?.next = head
var head2 = ListNode(4)
// head2.next = ListNode(5)
// head2.next?.next = ListNode(6)
// head2.next?.next?.next = head

head = getIntersectionNode(head1, head2) ?? ListNode(-1)
ListNode.printNode(head)
